package zhr.chapter9;

import javax.swing.*;
import java.awt.*;
import static javax.swing.SpringLayout.*;
public class SpringLayoutFrame extends JFrame {
    private SpringLayout springLayout;
    public SpringLayoutFrame(){
        springLayout = new SpringLayout();
        Container con =getContentPane(); //获得窗体容器对象
        this.setLayout(springLayout); //设置窗体对象为弹簧式布局

        JLabel topLabel = new JLabel("主题：");
        this.add(topLabel);
        springLayout.putConstraint(NORTH,topLabel,50,NORTH,con);
        springLayout.putConstraint(WEST,topLabel,50,WEST,con);
        JTextField textField=new JTextField();
        this.add(textField);
        springLayout.putConstraint(NORTH,textField,50,NORTH,con);
        springLayout.putConstraint(WEST,textField,50,EAST,topLabel);
        springLayout.putConstraint(EAST,textField,-50,EAST,con);

        JLabel buttonLabel = new JLabel("内容");
        this.add(buttonLabel);
        springLayout.putConstraint(NORTH,buttonLabel,50,SOUTH,textField);
        springLayout.putConstraint(WEST,buttonLabel,50,WEST,con);

        JScrollPane scrollPane=new JScrollPane();
        scrollPane.setViewportView(new JTextArea());
        this.add(scrollPane);
        springLayout.putConstraint(NORTH,scrollPane,50,SOUTH,textField);
        springLayout.putConstraint(WEST,scrollPane,50,EAST,buttonLabel);
        springLayout.putConstraint(EAST,scrollPane,-50,EAST,con);

        JButton resetButton =new JButton("清空");
        this.add(resetButton);
        springLayout.putConstraint(SOUTH,resetButton,-50,SOUTH,con);
        JButton submitButton = new JButton("确定");
        this.add(submitButton);
        springLayout.putConstraint(SOUTH,submitButton,-50,SOUTH,con);
        springLayout.putConstraint(EAST,submitButton,-50,EAST,con);
        springLayout.putConstraint(SOUTH,resetButton,-50,NORTH,submitButton);
        springLayout.putConstraint(EAST,resetButton,-50,WEST,submitButton);

        this.setBounds(400,400,400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("SpringLayout弹簧布局");
        this.setVisible(true);

    }
}
